//
//  FJCoinsViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/16.
//

import UIKit

class FJCoinsViewController: FJBaseViewController {

    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .text("My Coins").color(.black).font(19.font_bold).end()
    private let topView             = FJCoinsTopView()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        self.view.backgroundColor = "#FC9C05".toColor
        
        view.subviews{
            backButton
            titleLabel
            topView
            collectionView
        }
        
        backButton.top(.statusHeight + 7).leading(15).size(30)
        titleLabel.leading(60)
        titleLabel.CenterY == backButton.CenterY
        topView.leading(0).trailing(0).height(150)
        topView.Top == backButton.Bottom + 2
        collectionView.leading(0).trailing(0).bottom(.safe_bottom)
        collectionView.Top == topView.Bottom
        
        let itemW = (.screenWidth - 55.0)/2
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.itemSize = CGSize(width: itemW, height: itemW)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        layout.scrollDirection = .vertical
        collectionView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(FJCoinsCollectionItem.self, forCellWithReuseIdentifier: FJCoinsCollectionItem.description())
            v.showsHorizontalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}

extension FJCoinsViewController: UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: FJCoinsCollectionItem.description(), for: indexPath) as? FJCoinsCollectionItem else { return UICollectionViewCell() }
//        item.itemModel = dataSource[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let detailVC = LearningPicturesDetailViewController()
//        detailVC.itemModel = dataSource[indexPath.row]
//        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}

class FJCoinsCollectionItem: UICollectionViewCell {
    
    private let mainView = c_make_ex(UIView.self)
        .bgColor("#EFEFEF").corner(25).end()
    private let coinImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_Coins_coin").end()
    private let coinLabel = c_make_ex(UILabel.self).text("3826")
        .color("#FC9C05").font(19.font_heavy).alignment(.center).end()
    private let moneyLabel = c_make_ex(UIButton.self)
        .text("$ 0.99").color(.white).font(17.font_bold)
        .bgColor("#FC9C05").corner(20).end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.subviews {
            mainView.subviews{
                coinImageView
                coinLabel
                moneyLabel
            }
        }

        mainView.fillContainer()
        coinImageView.top(8.5).size(55).centerHorizontally()
        coinLabel.trailing(20).leading(20)
        coinLabel.Top == coinImageView.Bottom + 5
        moneyLabel.trailing(10).leading(10).bottom(6.5).height(40)
        
        moneyLabel.isUserInteractionEnabled = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class FJCoinsTopView: UIView{
    
    private let backImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_Coins_bg").end()
    private let coinImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_Coins_coin").end()
    private let coinLabel = c_make_ex(UILabel.self).text("3826")
        .color("#8D4100").font(40.font_bold).end()
    
//    var itemModel: FJSetupInfoModel = .none {
//        didSet{
//            mainView.image = itemModel.icon.toImage
//        }
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        subviews {
            backImageView.subviews{
                coinImageView
                coinLabel
            }
        }

        backImageView.trailing(20).leading(20).top(15).height(120)
        coinImageView.leading(75).size(65).centerVertically()
        coinLabel.trailing(20).centerVertically()
        coinLabel.Leading == coinImageView.Trailing + 5
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
